package com.guoliang.ucenterService.controller;

import com.guoliang.commonUtils.CommonResult;
import com.guoliang.commonUtils.JwtUtils;
import com.guoliang.ucenterService.entity.UcenterMember;
import com.guoliang.ucenterService.entity.vo.RegisterVo;
import com.guoliang.ucenterService.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-27
 */
@RestController
@RequestMapping("/educenter/ucenterMember")
public class UcenterMemberController {
    @Autowired
    UcenterMemberService memberService;
    @PostMapping("login")
    public CommonResult login(@RequestBody UcenterMember member){
        String token=memberService.login(member);
        return CommonResult.ok().data("token",token);
  }
    @ApiOperation("注册")
    @PostMapping("register")
    public CommonResult register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return CommonResult.ok();
    }
    @ApiOperation("根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public CommonResult getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return CommonResult.ok().data("userInfo", member);
    }
    @ApiOperation("根据用户id获取用户信息")
    @GetMapping("getMember/{id}")
    public com.guoliang.commonUtils.entity.UcenterMember getMember(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        com.guoliang.commonUtils.entity.UcenterMember ucenterMember=new com.guoliang.commonUtils.entity.UcenterMember();
        BeanUtils.copyProperties(member,ucenterMember);
        return ucenterMember;
    }
    @ApiOperation("根据某一天查询注册人数")
    @GetMapping("getRegisterNum/{day}")
    public CommonResult getRegisterNum(@PathVariable("day")String day){
        int nums=memberService.getRegisterNum(day);
        return CommonResult.ok().data("registerNum",nums);
    }
}
